package com.flyco.modules.oms.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.flyco.modules.oms.entity.OmsJddjOrder;
import com.flyco.modules.oms.entity.OmsJddjOrderProduct;
import com.flyco.modules.oms.entity.OmsJddjOrderInvoice;
import com.flyco.modules.oms.entity.OmsJddjOrderDiscount;
import com.flyco.modules.oms.mapper.OmsJddjOrderProductMapper;
import com.flyco.modules.oms.mapper.OmsJddjOrderInvoiceMapper;
import com.flyco.modules.oms.mapper.OmsJddjOrderDiscountMapper;
import com.flyco.modules.oms.mapper.OmsJddjOrderMapper;
import com.flyco.modules.oms.service.IOmsJddjOrderService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 京东到家订单表
 * @Author: flyco
 * @Date:   2021-08-24
 * @Version: V1.0
 */
@DS("store")
@Service
public class OmsJddjOrderServiceImpl extends ServiceImpl<OmsJddjOrderMapper, OmsJddjOrder> implements IOmsJddjOrderService {

	@Autowired
	private OmsJddjOrderMapper omsJddjOrderMapper;
	@Autowired
	private OmsJddjOrderProductMapper omsJddjOrderProductMapper;
	@Autowired
	private OmsJddjOrderInvoiceMapper omsJddjOrderInvoiceMapper;
	@Autowired
	private OmsJddjOrderDiscountMapper omsJddjOrderDiscountMapper;
	
	@Override
	@Transactional
	public void saveMain(OmsJddjOrder omsJddjOrder, List<OmsJddjOrderProduct> omsJddjOrderProductList,List<OmsJddjOrderInvoice> omsJddjOrderInvoiceList,List<OmsJddjOrderDiscount> omsJddjOrderDiscountList) {
		omsJddjOrderMapper.insert(omsJddjOrder);
		for(OmsJddjOrderProduct entity:omsJddjOrderProductList) {
			//外键设置
			entity.setJddjOrderId(omsJddjOrder.getId());
			omsJddjOrderProductMapper.insert(entity);
		}
		for(OmsJddjOrderInvoice entity:omsJddjOrderInvoiceList) {
			//外键设置
			entity.setJddjOrderId(omsJddjOrder.getId());
			omsJddjOrderInvoiceMapper.insert(entity);
		}
		for(OmsJddjOrderDiscount entity:omsJddjOrderDiscountList) {
			//外键设置
			entity.setJddjOrderId(omsJddjOrder.getId());
			omsJddjOrderDiscountMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(OmsJddjOrder omsJddjOrder,List<OmsJddjOrderProduct> omsJddjOrderProductList,List<OmsJddjOrderInvoice> omsJddjOrderInvoiceList,List<OmsJddjOrderDiscount> omsJddjOrderDiscountList) {
		omsJddjOrderMapper.updateById(omsJddjOrder);

		//1.先删除子表数据
		omsJddjOrderProductMapper.deleteByMainId(omsJddjOrder.getId());
		omsJddjOrderInvoiceMapper.deleteByMainId(omsJddjOrder.getId());
		omsJddjOrderDiscountMapper.deleteByMainId(omsJddjOrder.getId());

		//2.子表数据重新插入
		for(OmsJddjOrderProduct entity:omsJddjOrderProductList) {
			//外键设置
			entity.setJddjOrderId(omsJddjOrder.getId());
			omsJddjOrderProductMapper.insert(entity);
		}
		for(OmsJddjOrderInvoice entity:omsJddjOrderInvoiceList) {
			//外键设置
			entity.setJddjOrderId(omsJddjOrder.getId());
			omsJddjOrderInvoiceMapper.insert(entity);
		}
		for(OmsJddjOrderDiscount entity:omsJddjOrderDiscountList) {
			//外键设置
			entity.setJddjOrderId(omsJddjOrder.getId());
			omsJddjOrderDiscountMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		omsJddjOrderProductMapper.deleteByMainId(id);
		omsJddjOrderInvoiceMapper.deleteByMainId(id);
		omsJddjOrderDiscountMapper.deleteByMainId(id);
		omsJddjOrderMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			omsJddjOrderProductMapper.deleteByMainId(id.toString());
			omsJddjOrderInvoiceMapper.deleteByMainId(id.toString());
			omsJddjOrderDiscountMapper.deleteByMainId(id.toString());
			omsJddjOrderMapper.deleteById(id);
		}
	}

}
